package DAO;

import hibernate.AssetChangeInfo;
import hibernateUtil.HibernateUtil;

import java.sql.Date;
import java.util.List;
import java.util.Vector;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ZCTB {
	
	private int totalByQuery = 0;
	
	/*
	 * save资产信息
	 */
    public void addAssetTb(String parameter[])
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		AssetChangeInfo assetChangeInfo = new AssetChangeInfo();
		
		Date changeTime = java.sql.Date.valueOf(parameter[3]);
		
		assetChangeInfo.setAssetNo(parameter[0]);
		assetChangeInfo.setOriginalUnit(parameter[1]);
		assetChangeInfo.setNowUnit(parameter[2]);
		assetChangeInfo.setChangeTime(changeTime);
		assetChangeInfo.setName(parameter[4]);
		assetChangeInfo.setOriginalPrincipal(parameter[5]);
		assetChangeInfo.setNowPrincipal(parameter[6]);
		assetChangeInfo.setStatus(parameter[7]);
		assetChangeInfo.setEquipmentAttach(parameter[8]);
		assetChangeInfo.setRemark(parameter[9]);

		session.save(assetChangeInfo);
		trans.commit();    	
    }
    
    /*
     * 得到资产调拨信息的总记录
     */
    public int getTotal()
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		int countTotal = (session.createQuery("from AssetChangeInfo").list()).size(); //资产信息的总数;
		
		trans.commit();
		
		return countTotal;
    }   
    
    /*
     * 查询的资产调拨信息的总记录
     * 参数 -- 
     */
    public int getTotalByQuery()
    {
    	return totalByQuery;
    }  
    
    @SuppressWarnings("unchecked")    
    /*
     * 通过参数得到资产调拨的所有信息
     * 参数 start ， limit
     */
	public List getAllAssetTb(int start , int limit)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();

		Query query = session.createQuery("from AssetChangeInfo");
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List l = query.list();
		
		trans.commit();
		
		return l;
    }    
    
    @SuppressWarnings("unchecked")
    /*
     * 通过查询条件得到符合条件的资产调拨信息
     * 参数 --- start ,limit ,assetNo , assetName , purchaseUnit
     *         purchaseStartTime , purchaseEndingTime
     */
    public List getAssetTbByQuery(int start,int limit,String assetNo,String assetName,String originalUnit,String changeStartTime,String changeEndingTime)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		
		String str = "from AssetChangeInfo where ";
		
		if(!"".equals(assetNo))
		{
			str = str + "assetNo=:assetNo ";
		}
		if(!"".equals(assetName))
		{
			if(!"".equals(assetNo))
			{
				str = str + "or name=:assetName ";
			}
			else
			{
				str = str + "name=:assetName ";
			}
		}
		if(!"".equals(originalUnit))
		{
			if(!"".equals(assetNo) || !"".equals(assetName))
			{
				str = str + "or originalUnit=:originalUnit ";
			}
			else
			{
				str = str + "originalUnit=:originalUnit ";
			}
		}	
		if((!"".equals(changeStartTime)) && (!"".equals(changeEndingTime)))
		{
			if(!"".equals(assetNo) || !"".equals(assetName) || !"".equals(originalUnit))
			{
				str = str + "or changeTime between:startTime and:EndTime ";
			}	
			else
			{
				str = str + "changeTime between:startTime and:EndTime ";
			}			
		}
		
		Query query = session.createQuery(str);    //查询数据库
		
		if(!"".equals(assetNo))
		{
			query.setString("assetNo", assetNo);
		}
		if(!"".equals(assetName))
		{
			query.setString("assetName", assetName);
		}
		if(!"".equals(originalUnit))
		{
			query.setString("originalUnit", originalUnit);
		}		
		if(!"".equals(changeStartTime))
		{
			Date startUseTime_1 = java.sql.Date.valueOf(changeStartTime);
			query.setDate("startTime", startUseTime_1);
		}
		if(!"".equals(changeEndingTime))
		{
			Date EndTime_1 = java.sql.Date.valueOf(changeEndingTime);
			query.setDate("EndTime", EndTime_1);
		}		
		
		totalByQuery = query.list().size(); //通过查询条件，得到资产调拨信息的记录总数
		
		query.setFirstResult(start);    //查询记录从这一条开始；
		query.setMaxResults(limit); 	  //查询记录限制条数；
		List l = query.list();

		trans.commit();
		
		return l;
    }        

    /*
     * 通过资产编号(assetNo)更新资产出售信息
     * 参数 -- 
     */
    public void updataAssetTb(String parameter[])
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		
		AssetChangeInfo assetChangeInfo = (AssetChangeInfo)session.get(AssetChangeInfo.class, parameter[0]);
		
		Date changeTime = java.sql.Date.valueOf(parameter[3]);
		
		assetChangeInfo.setOriginalUnit(parameter[1]);
		assetChangeInfo.setNowUnit(parameter[2]);
		assetChangeInfo.setChangeTime(changeTime);
		assetChangeInfo.setName(parameter[4]);
		assetChangeInfo.setOriginalPrincipal(parameter[5]);
		assetChangeInfo.setNowPrincipal(parameter[6]);
		assetChangeInfo.setStatus(parameter[7]);
		assetChangeInfo.setEquipmentAttach(parameter[8]);
		assetChangeInfo.setRemark(parameter[9]);
		
		session.update(assetChangeInfo);
		
		trans.commit();
    }   
    
    @SuppressWarnings("unchecked")
    /*
     * 通过资产编号（assetNo）删除资产调拨信息
     */
	public void deleteAssetTb(Vector assetNo)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		
		for(int i=0; i<assetNo.size(); i++ )
		{
			AssetChangeInfo assetChangeInfo = (AssetChangeInfo)session.get(AssetChangeInfo.class, assetNo.elementAt(i).toString());
			session.delete(assetChangeInfo);
		}
		
		trans.commit();
    }     
}
